package com.noahplat.device.mapper;

import com.noahplat.device.pojo.Device;
import com.noahplat.device.util.CountDao;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * @InterfaceName: DeviceMapper
 * @Description: 设备mapper
 * @Author 李景星
 * @Date 2021/3/9
 * @Version 1.0
 */
public interface DeviceMapper extends Mapper<Device> {
    //根据账套ID查询设备信息
    @Select("select d.id,d.imei,d.dealer,d.prod,d.modelid,d.typeid,d.devcode,d.sup_location as supLocation,d.sup_temp as supTemp,d.sup_heart as supHeart,d.sup_blood as supBlood," +
            "d.sup_sleep as supSleep,d.sup_step as supStep,d.sup_oxygen as supOxygen,d.create_time as createTime,m.mymodel as model,t.type as type from tb_device d,enum_model m," +
            "enum_type t where m.id = d.modelid and t.id = d.typeid and d.dealer = #{dealer}")
    List<Device> getDeviceByDealer(@Param("dealer") Long dealer);

    //根据账套ID和imei查询设备信息
    @Select("select d.id,d.imei,d.dealer,d.prod,d.modelid,d.typeid,d.devcode,d.sup_location as supLocation,d.sup_temp as supTemp,d.sup_heart as supHeart,d.sup_blood as supBlood," +
            "d.sup_sleep as supSleep,d.sup_step as supStep,d.sup_oxygen as supOxygen,d.create_time as createTime,m.mymodel as model,t.type as type from tb_device d,enum_model m," +
            "enum_type t where m.id = d.modelid and t.id = d.typeid and d.dealer = #{dealer} and d.imei = #{imei}")
    List<Device> getDeviceByImei(@Param("dealer") Long dealer,@Param("imei") String imei);

    @Select("select d.id,d.imei,d.dealer,d.prod,d.modelid,d.typeid,d.devcode,d.sup_location as supLocation,d.sup_temp as supTemp,d.sup_heart as supHeart,d.sup_blood as supBlood," +
            "d.sup_sleep as supSleep,d.sup_step as supStep,d.sup_oxygen as supOxygen,d.create_time as createTime,m.mymodel as model,t.type as type from tb_device d,enum_model m," +
            "enum_type t where m.id = d.modelid and t.id = d.typeid and sup_location = 'Y' and d.dealer = #{dealer}")
    List<Device> getSiteDeviceByDealer(@Param("dealer") Long dealer);

    @Select("select d.id,d.imei,d.dealer,d.prod,d.modelid,d.typeid,d.devcode,d.sup_location as supLocation,d.sup_temp as supTemp,d.sup_heart as supHeart,d.sup_blood as supBlood," +
            "d.sup_sleep as supSleep,d.sup_step as supStep,d.sup_oxygen as supOxygen,d.create_time as createTime,m.mymodel as model,t.type as type from tb_device d,enum_model m," +
            "enum_type t where m.id = d.modelid and t.id = d.typeid and sup_location = 'Y' and d.dealer = #{dealer} and d.imei = #{imei}")
    List<Device> getSiteDeviceByImei(@Param("dealer") Long dealer,@Param("imei") String imei);

    @Insert("<script> INSERT INTO tb_device(imei,dealer,prod,modelid,typeid,devcode,sup_location,sup_temp,sup_heart," +
            "sup_blood,sup_sleep,sup_step,sup_oxygen,create_time) VALUES " +
            "<foreach collection = 'list' item='record' separator=','>" +
            "(#{record.imei},#{record.dealer},#{record.prod},#{record.modelid},#{record.typeid},#{record.devcode},#{record.supLocation}," +
            "#{record.supTemp},#{record.supHeart},#{record.supBlood},#{record.supSleep},#{record.supStep},#{record.supOxygen},#{record.prod}" +
            "#{record.createTime})</foreach></script>")
    int insertDevice(@Param("list") List<Device> list);

    @Select("select count(*) as count,dealer as dealer from tb_device group by dealer")
    List<CountDao> selectDeviceCount();
}
